export const openAPI = async <T>(
  apiConfig: FormTemplates,
  messages: Message[],
  handler: (data: ExtendedChatCompletionChunk) => void,
  controller?: AbortController
) => {
  try {
    const response = await useDefaultRequest.post(
      apiConfig["apiUrl"].value! + "/v1/chat/completions",
      {
        frequency_penalty: 1.15,
        max_tokens: 300,
        model: "gpt-3.5-turbo",
        presence_penalty: 1.15,
        stream: true,
        temperature: 1,
        top_p: 0.99,
        messages: messageHandler(messages),
      },
      {
        headers: {
          Authorization: `Bearer ${apiConfig["Authorization"].value}`,
        },
      },
      controller
    );

    if (response.status !== 200) {
      const error: Error = (await response.json()).error;
      console.log(error);
      
      handler(createErrorMessage(error.message));
      return;
    }
    useStream((response as Response).body!, handler);
  } catch (error: any) {
    handler(createErrorMessage(error.message));
  }
};
